<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%;"
  >
    <el-col :span="20" :class="{ strong: nodeData.manager === '负责人' }">
      <i v-if="nodeData.manager === '负责人'" class="el-icon-school"></i>
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end" style="text-align:right;">
        <el-col>
          <span>{{ nodeData.manager }}</span>
        </el-col>
        <el-col>
          <el-dropdown style="color:#000;" @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-color="'#3d9fff'" command="add">
                添加子部门
              </el-dropdown-item>
              <el-dropdown-item v-if="isRoot" v-color="'#3d9fff'" command="edit">
                编辑部门
              </el-dropdown-item>
              <el-dropdown-item v-if="isRoot" v-color="'#3d9fff'" command="del">
                删除部门
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDepartmentById } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    nodeData: {
      type: Object,
      required: true,
    },
    isRoot: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    // 一、dropdown下拉菜单的监听事件command
    async handleCommand(type) {
      if (type === 'add') {
        // 1.添加
        // 1.1 通知父组件弹出对话框
        this.$emit('addDept', this.nodeData)
      }
      if (type === 'edit') {
        // 2.编辑
        this.$emit('editDept', this.nodeData)
      }
      if (type === 'del') {
        // 3.删除
        // 3.1 弹出提示框提示是否删除,捕获用户选择确定还是取消
        try {
          await this.$confirm('亲,确定要删除吗?', '温馨提示', { type: 'warning' })
          await reqDelDepartmentById(this.nodeData.id)
          this.$message.success('删除成功')
          // 通知父组件更新信息
          this.$emit('delDept')
        } catch {
          return this.$message.info('取消')
        }
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.el-icon-school {
  vertical-align: middle;
  font-size: 20px;
  margin-right: 4px;
}
.strong {
  font-weight: bold;
}
</style>
